import { createReducer } from 'reduxsauce'
import { GLOBAL_SEARCH, SET_SEARCH_HISTORY } from '@ACTIONS/search'
import { SEARCH_TYPE } from '@CONSTANTS/search'
import { DEFAULT_TAKE } from '../sagas/search'

const INITIAL_STATE = {
  searchResult: {},
  searchDoctor: {},
  searchPainManagement: {},
  searchFeed: {},
  searchCourse: {},
  loading: false,
  canLoadMoreDoctor: false,
  canLoadMoreFeed: false,
  canLoadMoreCourse: false,
  canLoadMorePainManagement: false,
  refreshHistory: false,
  loadingDoctor: true,
  loadingFeedItem: true,
  loadingCourse: true,
  loadingPainManagement: true,
}

export default createReducer(INITIAL_STATE, {
  [GLOBAL_SEARCH.REQUEST]: (state, { params = {} }) => {
    const { skip, searchType } = params
    const {
      searchResult, searchDoctor, searchFeed, searchCourse, searchPainManagement
    } = state
    const isInit = skip === 0
    const isLoadMoreDoctor = searchType && searchType === SEARCH_TYPE.Doctor
    const isLoadMoreFeed = searchType && searchType === SEARCH_TYPE.FeedItem
    const isLoadMoreCourse = searchType && searchType === SEARCH_TYPE.Course
    const isLoadMorePainManagement = searchType && searchType === SEARCH_TYPE.PainManagementTemplate
    return {
      ...state,
      loading: isInit,
      searchResult: isInit && !searchType ? {} : searchResult,
      searchDoctor: isInit && isLoadMoreDoctor ? {} : searchDoctor,
      searchPainManagement: isInit && isLoadMorePainManagement ? {} : searchPainManagement,
      searchFeed: isInit && isLoadMoreFeed ? {} : searchFeed,
      searchCourse: isInit && isLoadMoreCourse ? {} : searchCourse,
      loadingDoctor: true,
      loadingFeedItem: true,
      loadingCourse: true,
      loadingPainManagement: true,
    }
  },
  [GLOBAL_SEARCH.SUCCESS]: (state, { result = {}, searchType, skip }) => {
    const {
      searchResult, searchDoctor, searchFeed, searchCourse, searchPainManagement
    } = state
    const { Doctor = {}, FeedItem = {}, Course = {}, PainManagementTemplates: PainManagement } = result
    const { Doctors } = Doctor
    const { FeedItems } = FeedItem
    const { Courses } = Course
    const { PainManagementTemplates = [] } = PainManagement || {}
    const isLoadMoreDoctor = searchType && searchType === SEARCH_TYPE.Doctor
    const isLoadMoreFeed = searchType && searchType === SEARCH_TYPE.FeedItem
    const isLoadMoreCourse = searchType && searchType === SEARCH_TYPE.Course
    const isLoadMorePainManagement = searchType && searchType === SEARCH_TYPE.PainManagementTemplate
    let newDoctorResult = searchDoctor
    console.log('11111111111111', searchType, skip)
    if (skip === 0 && isLoadMoreDoctor || !searchType) {
      newDoctorResult = Doctor
    } else if (isLoadMoreDoctor) {
      newDoctorResult = {
        ...searchDoctor,
        Doctors: [...searchDoctor.Doctors, ...Doctors]
      }
    }
    let newFeedResult = searchFeed
    if (skip === 0 && isLoadMoreFeed || !searchType) {
      newFeedResult = FeedItem
    } else if (isLoadMoreFeed) {
      newFeedResult = {
        ...searchFeed,
        FeedItems: [...searchFeed.FeedItems, ...FeedItems]
      }
    }
    console.log('newCourseResult', searchCourse, isLoadMoreCourse)
    let newCourseResult = searchCourse
    if (skip === 0 && isLoadMoreCourse || !searchType) {
      newCourseResult = Course
    } else if (isLoadMoreCourse) {
      newCourseResult = {
        ...searchCourse,
        Courses: [...searchCourse.Courses, ...Courses]
      }
    }

    let newPainManagementResult = searchPainManagement
    if (skip === 0 && isLoadMorePainManagement || !searchType) {
      newPainManagementResult = PainManagement
    } else if (isLoadMorePainManagement) {
      newPainManagementResult = {
        ...searchPainManagement,
        PainManagementTemplates: [...searchPainManagement.PainManagementTemplates, ...PainManagementTemplates]
      }
    }

    return {
      ...state,
      loading: false,
      searchResult: skip === 0 && !searchType ? result : searchResult,
      searchDoctor: newDoctorResult,
      searchFeed: newFeedResult,
      searchCourse: newCourseResult,
      searchPainManagement: newPainManagementResult,
      canLoadMoreDoctor: isLoadMoreDoctor || !searchType ? Doctors?.length >= DEFAULT_TAKE : state.canLoadMoreDoctor,
      canLoadMoreFeed: isLoadMoreFeed || !searchType ? FeedItems?.length >= DEFAULT_TAKE : state.canLoadMoreFeed,
      canLoadMoreCourse: isLoadMoreCourse || !searchType ? Courses?.length >= DEFAULT_TAKE : state.canLoadMoreCourse,
      canLoadMorePainManagement: (isLoadMorePainManagement || !searchType
        ? PainManagementTemplates?.length >= DEFAULT_TAKE
        : state.canLoadMorePainManagement),
      loadingDoctor: false,
      loadingFeedItem: false,
      loadingCourse: false,
      loadingPainManagement: false,
    }
  },
  [GLOBAL_SEARCH.FAILURE]: (state, { searchType }) => ({
    ...state,
    loading: false,
    [`loading${searchType}`]: false
  }),

  [SET_SEARCH_HISTORY.SUCCESS]: (state, { refreshHistory }) => ({
    ...state,
    refreshHistory,
  }),
})
